<?php
namespace app\admin\controller\soft;
use app\providers\BaseController;
use think\Image;
use app\admin\model\SystemUpload;
use app\admin\model\SystemUploadClassify;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("文件目录")
 * @Apidoc\Group("base")
 * @Apidoc\Sort(2)
 */
class UploadClassify extends BaseController 
{
	
	public $model;
	
	/**
    * 构造函数自动执行
    * @return null
    */
    public function __construct(){
    	$this->model = new SystemUploadClassify;
    }
	
   	/**
    * 获取目录
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/upload_classify/getTree")
    * @Apidoc\Method("POST")
    */
	public function getTree(){
		$result =  $this->model->order('sort_no','desc')->select()->toArray();
		if( count($result) == 0 ){
			$this->model->save(['name'=>'全部文件']);
			$result = $this->model->order('sort_no','desc')->select()->toArray();
		}
		return \Api::ReSucess($result,"操作成功！");
	}
	
	
    /**
    * 新增
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/upload_classify/add")
    * @Apidoc\Method("POST")
    * @Apidoc\Param("pid", require=true, default="",desc="父id" )
    * @Apidoc\Param("name", require=true, default="",desc="名称" )
    */
	public function add($pid,$name){
		$this->model->save(['name'=>$name,'pid'=>$pid]);
		return \Api::ReSucess( $this->model->id ,"操作成功！");
	}
	
   	/**
    * 编辑
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/upload_classify/edit")
    * @Apidoc\Method("POST")
    * @Apidoc\Param("id", require=true, default="",desc="id" )
    * @Apidoc\Param("name", require=true, default="",desc="名称" )
    */
	public function edit($id,$name){
		$res = $this->model->where('id',$id)->update(['name'=>$name]);
		return \Api::ReSucess($res,"操作成功！");
	}

   	/**
    * 删除
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/upload_classify/edit")
    * @Apidoc\Method("POST")
    * @Apidoc\Param("ids", require=true, default="",desc="ids" )
    */
	public function delete($ids){
		if( empty($ids)  )
			throw new \think\Exception('ids参数不能为空');
		
		$result = SystemUpload::where("classify_id",'in',$ids)->select();
		if($result[0]){
			return $this -> ReError("该节点或子节点下存在文件数据，不允许删除！");
		}	
		$result = $this->model->where('id','in',$ids)->delete();
		if( !$result ){
			return \Api::ReError("删除失败！");
		}else{
			return \Api::ReSucess($result,"删除成功！");
		}
		
	}
	
	 /**
     * 排序调整
     * @Apidoc\Desc("排序调整")
     * @Apidoc\Author("weifashi")
     * @Apidoc\Url("admin/soft/upload_classify/sortMove")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("moveId",type="int",require=true,default="1",desc="id1" )
     * @Apidoc\Param("coverId", type="int",require=true, default="2",desc="id2" )
     */
	public function sortMove($moveId,$coverId){
		if( empty($moveId) )
			throw new \think\Exception('moveId参数不能为空');
		if( empty($coverId) )
			throw new \think\Exception('coverId参数不能为空');
		
		if($moveId==-1 || $coverId==-1){
			return $this->ReSucess("","操作成功");
		} 
		
		$result = model('SystemGroup')->updateSort($moveId,$coverId);
		if($result){
			return \Api::ReSucess("","操作成功");
		}else{
			return \Api::ReError("未知错误！");
		}
		
	}
	
	 /**
     * 获取授权
     * @Apidoc\Author("weifashi")
     * @Apidoc\Url("admin/soft/upload_classify/getAction")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("org_id",type="int",require=true,default="1",desc="组织id" )
     */
	public function getAction($org_id){
		$result = model('SystemGroup')->where('org_id',$org_id)->value('action');
		return \Api::ReSucess($result,"操作成功");
	}
	
}